#coding=utf-8
import uliweb.form as form

class UploadForm(form.Form):
    file = form.FileField('导入文件', required=True)

class ResourceEditForm(form.Form):
    def form_validate(self, data):
        from sqlalchemy.sql import select, func
        from uliweb.orm import get_model, do_
        from decimal import Decimal
        
        error = {}
        
        Resource = get_model('resource')
        r = do_(select([func.sum(Resource.c.workload)], (Resource.c.user == self.object._user_) & (Resource.c.id!=self.object.id) & (Resource.c.delete_flag==False)))
        sum = list(r)[0][0] or Decimal('0.0')
        total = sum + Decimal(data['workload'])
        if  total> Decimal('1.0'):
            error['workload'] = '工作量总计不能超过1.0，但目前已达%s，请修改。' % str(total)
        return error
